package dao;

import org.apache.ibatis.annotations.*;
import pojo.Jjzs;

import java.util.List;

@Mapper
/**
 * 急救知识表数据访问接口
 */
public interface JjzsMapper {
    // 根据id查询急救知识
    @Select("SELECT " +
            "id, " +
            "title, " +
            "content, " +
            "image_url, " +
            "view_count, " +
            "create_time " +
            "FROM jjzs WHERE id = #{id}")
    Jjzs findJjzsById(Integer id);

    // 查询所有急救知识
    @Select("SELECT " +
            "id, " +
            "title, " +
            "content, " +
            "image_url, " +
            "view_count, " +
            "create_time " +
            "FROM jjzs ORDER BY create_time DESC")
    List<Jjzs> findAllJjzs();

    // 按访问量查询热门急救知识
    @Select("SELECT " +
            "id, " +
            "title, " +
            "content, " +
            "image_url, " +
            "view_count, " +
            "create_time " +
            "FROM jjzs ORDER BY view_count DESC LIMIT #{limit}")
    List<Jjzs> findHotJjzs(@Param("limit") int limit);

    // 搜索急救知识
    @Select("SELECT " +
            "id, " +
            "title, " +
            "content, " +
            "image_url, " +
            "view_count, " +
            "create_time " +
            "FROM jjzs WHERE " +
            "title LIKE CONCAT('%', #{keyword}, '%') OR " +
            "content LIKE CONCAT('%', #{keyword}, '%')")
    List<Jjzs> searchJjzs(String keyword);

    // 新增急救知识
    @Insert("INSERT INTO jjzs (title, content, image_url, view_count, create_time) " +
            "VALUES (#{title}, #{content}, #{imageUrl}, #{viewCount}, NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int addJjzs(Jjzs jjzs);

    // 更新急救知识
    @Update("UPDATE jjzs SET " +
            "title = #{title}, " +
            "content = #{content}, " +
            "image_url = #{imageUrl} " +
            "WHERE id = #{id}")
    int updateJjzs(Jjzs jjzs);

    // 更新访问量
    @Update("UPDATE jjzs SET view_count = view_count + 1 WHERE id = #{id}")
    int incrementViewCount(Integer id);

    // 删除急救知识
    @Delete("DELETE FROM jjzs WHERE id = #{id}")
    int deleteJjzs(Integer id);
}